<?php

namespace app\models;

use Phalcon\Mvc\Collection;
use Phalcon\Mvc\Model\Validator\Inclusionin;
use Phalcon\Mvc\Model\Validator\Numericality;

class RobotsMongo extends Collection
{
    public function initialize()
    {
        $this->setConnectionService('mongo');   //可以显示指定连接，默认为mongo
    }

    public function validation()
    {
        $this->validate(new Inclusionin([
            'field' => 'type',
            'message' => 'Type must be:mechanical or virtual',
            'domain' => ['mechanical', 'virtual']
        ]));
        $this->validate(new Numericality([
            'field' => 'price',
            'message' => 'price must be numeric'
        ]));
        return $this->validationHasFailed() != true;
    }

    /**
     * 在这里可以做验证，插入到数据库的字段限制
     */
    public function beforeSave()
    {
//        exit('aaa');
    }

    public function getSource()
    {
        return 'robots';
    }

    public function notSave()
    {
        $flash = $this->getDI()->getShared('flash');
        foreach ($this->getMessages() as $message) {
            $flash->error(strval($message));
        }
    }
}